package cn.tqfeiyang.smartadmin.commons.application;

import cn.tqfeiyang.smartadmin.commons.pagination.OrderDirectionConverter;
import cn.tqfeiyang.smartadmin.commons.security.SecurityInterceptor;
import cn.tqfeiyang.smartadmin.commons.security.SecurityService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvc配置
 *
 * @author tqfeiyang
 * @since 2024-07-19
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Resource
    private SecurityInterceptor securityInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(securityInterceptor)
                .excludePathPatterns(SwaggerConfiguration.SWAGGER_WHITELIST)
                .excludePathPatterns(SecurityService.URL_WHITELIST)
                .addPathPatterns("/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new OrderDirectionConverter());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*");
    }
}
